package cn.gson.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gson.crm.model.dao.ConstactOrderDAO;
import cn.gson.crm.model.dao.CustomerinfoIDAO;
import cn.gson.crm.model.dao.ProductDao;
import cn.gson.crm.model.dao.SendGoodsDetailsDAO;
import cn.gson.crm.model.pojo.ConstactOrder;
import cn.gson.crm.model.pojo.Customerinfo;
import cn.gson.crm.model.pojo.Product;
import cn.gson.crm.model.pojo.Sendgoodsdetail;
import cn.gson.crm.model.vo.ConstactOrderVO;
import cn.gson.crm.model.vo.CustomerVO;
import cn.gson.crm.model.vo.SendgoodsdetailVO;

@Service
@Transactional
public class SendGoodsDetailsService {
	@Autowired
	private SendGoodsDetailsDAO sDao;
	@Autowired
	private CustomerinfoIDAO cIdao;
	@Autowired
	private ConstactOrderDAO cDao;
	@Autowired
	private ProductDao pDao;

	/**
	 * 获取所有交付记录
	 * 
	 * @return
	 */
	public List<SendgoodsdetailVO> getAllSgd(int page, int rows) {
		PageRequest pageable = new PageRequest(page - 1, rows);
		List<Sendgoodsdetail> listpo = sDao.getAllSgd(pageable);
		for (Sendgoodsdetail s : listpo) {
			System.out.println(s.getSgdId());
		}
		List<SendgoodsdetailVO> listvo = new ArrayList<>();
		for (Sendgoodsdetail s : listpo) {
			listvo.add(returnSgdVO(s));
		}
		return listvo;
	}

	public SendgoodsdetailVO returnSgdVO(Sendgoodsdetail c) {
		SendgoodsdetailVO sVo = new SendgoodsdetailVO(c.getSgdId(), c.getSgdCoId(), c.getSgdCoTheme(), c.getSgdCost(),
				c.getSgdCusId(), c.getSgdCusName(), c.getSgdNumber(), c.getSgdProId(), c.getSgdProName(),
				c.getSgdProPrice(), c.getSgdRemarks(), c.getSgdTime());
		Customerinfo customerinfo = c.getCustomerinfo();
		ConstactOrder cOrder = c.getConstactOrder();
		if (customerinfo != null) {
			CustomerVO vo = new CustomerVO(customerinfo.getCustId(), customerinfo.getCustName());
			sVo.setCoCusIdvo(vo.getCustId());
			sVo.setCoCusNamevo(vo.getCustName());
			sVo.setcVo(vo);
		}
		if (cOrder != null) {
			ConstactOrderVO cVo = new ConstactOrderVO(cOrder.getCoId(), cOrder.getCoTheme());
			sVo.setOrderIdvo(cVo.getCoId());
			sVo.setOrderThemevo(cVo.getCoTheme());
			sVo.setcOrderVO(cVo);
		}
		return sVo;
	}

	/**
	 * 添加交付记录
	 * 
	 * @param sgd
	 * @param cusId
	 * @param oId
	 */
	public void addSgd(Sendgoodsdetail sgd, Long cusId, Long oId) {
		try {
			Product product = pDao.findOne(sgd.getSgdProId().longValue());
			if (product != null) {
				sgd.setSgdProName(product.getProductName());
			}
			if (sgd.getSgdProPrice() != null) {
				sgd.setSgdCusId(sgd.getSgdProPrice().multiply(sgd.getSgdNumber()));
			}
			Customerinfo customerinfo = cIdao.findOne(cusId);
			ConstactOrder constactOrder = cDao.findOne(oId);
			sgd.setCustomerinfo(customerinfo);
			sgd.setConstactOrder(constactOrder);
			sDao.save(sgd);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 总记录条数
	 * 
	 * @return
	 */
	public Long count() {
		return sDao.count();
	}

	/**
	 * 删除交付记录
	 * @param id
	 */
	public void deleteSgd(Long id) {
		try {
			Sendgoodsdetail sgd = sDao.findOne(id);
			sDao.delete(sgd);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
}
